#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/10/18 22:30
# @File     : kc16_23.py
# @Project  : CSPT
def parse_23_Charging_Process_BMS_Demand_and_Charger_Output(body):
    # 解析0x1B 错误报文
    data = {
        "交易流水号": f"{body[0:32]}({body[0:32]})",
        "桩编号": f"{body[32:46]}({body[32:46]})",
        "枪号": f"{int(body[46:48], 16)}枪({body[46:48]})",
        "BMS 电压需求": f"{int(body[48:52], 16) / 10}V ({body[48:52]})",
        "BMS 电流需求": f"{int(body[52:56], 16) / 10 - 400}A (偏移量:-400A)({body[52:56]})",
        "BMS 充电模式": f"{charge_mode(body[56:58])}({body[56:58]})",
        "BMS 充电电压测量值": f"{int(body[58:62], 16) / 10}V ({body[58:62]})",
        "BMS 充电电流测量值": f"{int(body[62:66], 16) / 10 - 400}A (偏移量:-400A)({body[62:66]})",
        "BMS 最高单体动力蓄电池电压及组号": f"{int(body[66:70], 16) / 10}({body[66:70]})",
        "BMS 当前荷电状态 SOC (%)": f"{int(body[70:72], 16)}% ({body[70:72]})",
        "BMS 估算剩余充电时间": f"{int(body[72:76], 16)}min ( ({body[72:76]})",
        "电桩电压输出值": f"{int(body[76:80], 16) / 10}V ({body[76:80]})",
        "电桩电流输出值": f"{int(body[80:84], 16) / 10 - 400}A (偏移量:-400A)({body[80:84]})",
        "累计充电时间": f"{int(body[84:88], 16)}min ( ({body[84:88]})",
    }
    formatted_data = ""
    for key, value in data.items():
        formatted_data += f"{key}: {value}\n"
    return formatted_data


def charge_mode(result):
    return "恒压充电" if result == "01" else "恒流充电" if result == "02" else "未知"


def parse_bit_data(hex_data):
    # 将十六进制字符串转换为整数
    data = int(hex_data, 16)  # 将16进制数据转为整数

    # 取[1-12]位 (电压)
    voltage_mask = 0xFFF0  # 掩码，用于获取低12位
    voltage = data & voltage_mask  # 位与操作获取电压值
    voltage_decimal = voltage  # 直接得到十进制电压

    # 取[12-16]位 (电池组号)
    battery_group_number = (data >> 12) & 0x000F  # 位移12位后取4位

    return voltage_decimal, battery_group_number


__all__ = ['parse_23_Charging_Process_BMS_Demand_and_Charger_Output']
